package com.googlecode.rjm.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

@Entity
@Table(name = "versions")
public class Version extends Persistent implements Comparable<Version> {

	private static final long serialVersionUID = 8782763895035598724L;

	@Id
	private Integer id;

	private String name;

	private String description;

	@ManyToOne
	@JoinColumn(name = "project_id")
	private Project project;

	@OneToMany(mappedBy = "version")
	@OrderBy(value = "subject")
	private List<Issue> issues;

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Project getProject() {
		return this.project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public List<Issue> getIssues() {
		return this.issues;
	}

	public void setIssues(List<Issue> issues) {
		this.issues = issues;
	}

	public String toString() {
		return getProject().getName() + ": " + getName();
	}

	public int compareTo(Version o) {
		return this.toString().compareTo(o.toString());
	}

	public Date getStartDate() {
		Date startDate = null;
		boolean first = true;
		for (Issue issue : getIssues()) {
			if (issue.getStartDate() != null) {
				if (first) {
					startDate = issue.getStartDate();
					first = false;
				} else if (issue.getStartDate().before(startDate)) {
					startDate = issue.getStartDate();
				}
			}
		}
		return startDate;
	}

	public Date getDueDate() {
		Date dueDate = null;
		boolean first = true;
		for (Issue issue : getIssues()) {
			if (issue.getDueDate() != null) {
				if (first) {
					dueDate = issue.getDueDate();
					first = false;
				} else if (issue.getDueDate().after(dueDate)) {
					dueDate = issue.getDueDate();
				}
			}
		}
		return dueDate;
	}

	public Double getEstimatedTimeAtualizado() {
		Double estimatedTime = 0.0;
		for (Issue issue : getIssues()) {
			if (issue.getEstimatedHours() != null) {
				estimatedTime += issue.getEstimatedHours();
			}
		}
		return estimatedTime;
	}

	public Double getSpentTime() {
		Double spentTime = 0.0;
		for (Issue issue : getIssues()) {
			for (SpentTime spent : issue.getSpentTime()) {
				spentTime += spent.getTime();
			}
		}
		return spentTime;
	}

	public Integer getOpenedTickets() {
		Integer openedTickets = 0;
		for (Issue issue : getIssues()) {
			if (!issue.getStatus().isClosed()) {
				openedTickets++;
			}
		}
		return openedTickets;
	}

	public Integer getClosedTickets() {
		Integer closedTickets = 0;
		for (Issue issue : getIssues()) {
			if (issue.getStatus().isClosed()) {
				closedTickets++;
			}
		}
		return closedTickets;
	}
}
